/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package View;

import Dao.SistemaDAO;
import Modelo.*;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Maicom
 */
public class LocReceita extends javax.swing.JFrame {

    SistemaDAO sisDAO = new SistemaDAO();
    ArrayList<ItemReceita> itemRec = new ArrayList<ItemReceita>();
    ArrayList<Receita> listaRec = new ArrayList<Receita>();
    Receita rec = new Receita();
    TelaPrincipal tp = new TelaPrincipal();
    public LocReceita(TelaPrincipal tp) {
        initComponents();
        this.tp = tp;
        setModelColumn();

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPnTitulo = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jBtPesquisar = new javax.swing.JButton();
        jTxtCriterio = new javax.swing.JTextField();
        jCBRegraPesq = new javax.swing.JComboBox();
        jPanel4 = new javax.swing.JPanel();
        jBtSair = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTbDados = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Localizar Médico");
        setBackground(new java.awt.Color(255, 255, 255));
        setResizable(false);

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));

        jPnTitulo.setBackground(new java.awt.Color(255, 255, 255));
        jPnTitulo.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        jLabel1.setText("Localizar Produto");

        javax.swing.GroupLayout jPnTituloLayout = new javax.swing.GroupLayout(jPnTitulo);
        jPnTitulo.setLayout(jPnTituloLayout);
        jPnTituloLayout.setHorizontalGroup(
            jPnTituloLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPnTituloLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel1)
                .addGap(125, 125, 125))
        );
        jPnTituloLayout.setVerticalGroup(
            jPnTituloLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPnTituloLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel1)
                .addContainerGap())
        );

        jPanel3.setBackground(new java.awt.Color(255, 255, 255));
        jPanel3.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        jLabel2.setText("Regras de Pesquisa");

        jLabel3.setText("Critério");

        jBtPesquisar.setForeground(new java.awt.Color(204, 204, 204));
        jBtPesquisar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/bonecoPesquisaPequeno.png"))); // NOI18N
        jBtPesquisar.setBorder(null);
        jBtPesquisar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtPesquisarActionPerformed(evt);
            }
        });

        jCBRegraPesq.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Código igual " }));
        jCBRegraPesq.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jCBRegraPesqItemStateChanged(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jCBRegraPesq, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(jTxtCriterio))
                .addGap(30, 30, 30)
                .addComponent(jBtPesquisar)
                .addGap(18, 18, 18))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jTxtCriterio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jCBRegraPesq, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(jBtPesquisar))
                .addContainerGap(19, Short.MAX_VALUE))
        );

        jPanel4.setBackground(new java.awt.Color(255, 255, 255));
        jPanel4.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        jBtSair.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/SairPequeno.png"))); // NOI18N
        jBtSair.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtSairActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addGap(186, 186, 186)
                .addComponent(jBtSair, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addComponent(jBtSair, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(31, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        jTbDados.setAutoCreateRowSorter(true);
        jTbDados.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Title 1", "Title 2", "Título 3", "Título 4"
            }
        ));
        jTbDados.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTbDadosMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTbDados);
        jTbDados.getColumnModel().getColumn(0).setMinWidth(65);
        jTbDados.getColumnModel().getColumn(0).setPreferredWidth(65);
        jTbDados.getColumnModel().getColumn(0).setMaxWidth(65);
        jTbDados.getColumnModel().getColumn(2).setMinWidth(65);
        jTbDados.getColumnModel().getColumn(2).setPreferredWidth(65);
        jTbDados.getColumnModel().getColumn(2).setMaxWidth(65);
        jTbDados.getColumnModel().getColumn(3).setMinWidth(65);
        jTbDados.getColumnModel().getColumn(3).setPreferredWidth(65);
        jTbDados.getColumnModel().getColumn(3).setMaxWidth(65);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 425, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 214, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPnTitulo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPnTitulo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(28, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );

        pack();
        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        java.awt.Dimension dialogSize = getSize();
        setLocation((screenSize.width-dialogSize.width)/2,(screenSize.height-dialogSize.height)/2);
    }// </editor-fold>//GEN-END:initComponents

    private void jCBRegraPesqItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jCBRegraPesqItemStateChanged
        setModelColumn();
        jTxtCriterio.setText("");
    }//GEN-LAST:event_jCBRegraPesqItemStateChanged

    private void jTbDadosMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTbDadosMouseClicked
        if (evt.getClickCount() >= 2) {
            TabelaReceitaFarm lm = (TabelaReceitaFarm) jTbDados.getModel();

            rec = lm.getSelectedReceita(jTbDados.getSelectedRow());

            CadReceita cadReceita = new CadReceita(this, rec, true, null, null);
            cadReceita.setVisible(true);
        }
    }//GEN-LAST:event_jTbDadosMouseClicked

    private void jBtPesquisarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtPesquisarActionPerformed
        /*
         * Código igual a Nome iniciando Nome contendo
         */

        List<Receita> lista = new ArrayList<Receita>();

        String[] group = null;
        String[] order = null;

        if (jTxtCriterio.getText().trim().equals("") || jTxtCriterio.getText() == null) {
            lista = (ArrayList<Receita>) sisDAO.listar("Modelo.Receita");

            setModelColumn();

            for (Receita rec : lista) {
                TabelaReceitaFarm lm = (TabelaReceitaFarm) jTbDados.getModel();
                lm.addReceita(rec);
            }

            return;
        }

        switch (jCBRegraPesq.getSelectedIndex()) {
            case 0: {
                lista = (ArrayList<Receita>) sisDAO.listaCondicao("Modelo.Receita", jTxtCriterio.getText(), group, order, "idreceita", 0);
                break;
            }
//            case 1: {
//                lista = (ArrayList<Produto>) sisDAO.listaCondicao("Modelo.Produto", jTxtCriterio.getText(), group, order, "descricao", 1);
//                break;
//            }
//            case 2: {
//                lista = (ArrayList<Produto>) sisDAO.listaCondicao("Modelo.Produto", jTxtCriterio.getText(), group, order, "descricao", 2);
//                break;
//            }
//
//            case 3: {
//                lista = (ArrayList<Produto>) sisDAO.listaCondicao("Modelo.Produto", jTxtCriterio.getText(), group, order, "codbarra", 1);
//                break;
//            }

        }

        setModelColumn();

        for (Receita rec : lista) {
            TabelaReceitaFarm lm = (TabelaReceitaFarm) jTbDados.getModel();
            lm.addReceita(rec);
        }
    }//GEN-LAST:event_jBtPesquisarActionPerformed

    public void setModelColumn() {
        jTbDados.setModel(new TabelaReceitaFarm());
        jTbDados.getColumnModel().getColumn(0).setPreferredWidth(65);
        jTbDados.getColumnModel().getColumn(1).setPreferredWidth(150);
        jTbDados.getColumnModel().getColumn(2).setPreferredWidth(150);
        jTbDados.getColumnModel().getColumn(3).setPreferredWidth(70);

    }
    private void jBtSairActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtSairActionPerformed
        this.dispose();
    }//GEN-LAST:event_jBtSairActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBtPesquisar;
    private javax.swing.JButton jBtSair;
    private javax.swing.JComboBox jCBRegraPesq;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPnTitulo;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTbDados;
    private javax.swing.JTextField jTxtCriterio;
    // End of variables declaration//GEN-END:variables
}
